﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FaceSharp.Api;

namespace FaceSharp.Samples.Presenter
{
    public class PhotoBrowserPresenter
    {
        private readonly View.IPhotoAlbum _photoAlbumView;
        private readonly FaceSharp.Api.FacebookApi _facebookApi;
        private readonly IEnumerable<View.AlbumDetail> _albumDetails;

        public PhotoBrowserPresenter(View.IPhotoAlbum photoAlbumView)
        {
            _photoAlbumView = photoAlbumView;
            _facebookApi = new FacebookApi();
            var albums = _facebookApi.Album.List();
            if(albums != null)
            {
                _albumDetails = (from a in albums
                                 select new View.AlbumDetail() {AlbumId = long.Parse(a.Aid), AlbumName = a.Name}).
                    AsEnumerable();
                _photoAlbumView.Albums = _albumDetails;
                
            }  
        }

        public void ViewPhotos()
        {
            var fbphotos = (from p in _facebookApi.Album.GetPhotos(_photoAlbumView.AlbumId)
                           select new View.PhotoDetail() {Caption = p.Name, PhotoUrl = p.Source}).AsEnumerable();

            _photoAlbumView.Photos = fbphotos;
            _photoAlbumView.AlbumName =
                _albumDetails.Where(x => x.AlbumId.ToString() == _photoAlbumView.AlbumId).FirstOrDefault().AlbumName;
        }
    }
}
